home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / MSG Demo 1.4.source Folder / Demo ƒ / Shell ƒ / file interface.c < prev    next >
Text File  |  1994-04-22  |  5KB  |  162 lines

  1. /**********************************************************************\
  2.  
  3. File:        file interface.c
  4.  
  5. Purpose:    This module handles the standard file package (open & save
  6.             dialogs) and returns an FSSpec to deal with.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program in a file named "GNU General Public License".
  20. If not, write to the Free Software Foundation, 675 Mass Ave,
  21. Cambridge, MA 02139, USA.
  22.  
  23. \**********************************************************************/
  24.  
  25. #include "Script.h"
  26. #include "file interface.h"
  27. #include "environment.h"
  28. #include "util.h"
  29.  
  30. Boolean GetSourceFile(FSSpec *editFile)
  31. /* a standard procedure which shows an open dialog box and returns the FSSpec of
  32.    the file you selected (or returns FALSE if you cancelled) */
  33. {
  34.     Point                where;
  35.     OSErr                isHuman;
  36.     StandardFileReply    reply;
  37.     SFReply                oldReply;
  38.     FInfo                fndrInfo;
  39.     long                procID;
  40.     int                    i;
  41.     SFTypeList            theTypes;
  42.     
  43.     theTypes[0]='TEXT';
  44.     theTypes[1]='????';
  45.     isHuman=FALSE;            /* to err is human... */
  46.     if (gStandardFile58)    /* in system 7 or later, all the work is done for us */
  47.         StandardGetFile(0L, 2, theTypes, &reply);    /* reply's got an FSSpec in it afterwards */
  48.     else    /* pre-system-7, we have to do a bit of work manually */
  49.     {
  50.         where.h = (screenBits.bounds.right - 348)/2;    /* center horizontally */
  51.         where.v = (screenBits.bounds.bottom - 200)/3;    /* 1:3 vertically */
  52.         SFGetFile(where, "\p", 0L, 2, theTypes, 0L, &oldReply);    /* old routine */
  53.         
  54.         reply.sfGood = oldReply.good;
  55.         if (reply.sfGood)    /* if user selected a file */
  56.         {
  57.             reply.sfType = oldReply.fType;    /* type of file */
  58.             isHuman=GetWDInfo(oldReply.vRefNum, &reply.sfFile.vRefNum,    /* get vRefNum */
  59.                                 &reply.sfFile.parID, &procID);            /* and parID */
  60.             if (isHuman!=noErr)
  61.             {
  62.                 reply.sfFile.vRefNum = oldReply.vRefNum;    /* default to old method */
  63.                 reply.sfFile.parID = 0;                        /* & 0 parID -- let the */
  64.             }                                                /* system figure it out */
  65.             
  66.             /* get the name */
  67.             Mymemcpy(reply.sfFile.name, oldReply.fName, oldReply.fName[0]+1);
  68.             
  69.             /* get the finder flags of the selected file -- I've never used them,
  70.                but it's just for completeness; StandardGetFile() will retrieve
  71.                this info, so we should fake it for consistency */
  72.             reply.sfScript=smSystemScript;
  73.             isHuman=HGetFInfo(reply.sfFile.vRefNum, reply.sfFile.parID,
  74.                                 reply.sfFile.name, &fndrInfo);
  75.             reply.sfFlags=(isHuman==noErr) ? fndrInfo.fdFlags : 0;
  76.             
  77.             reply.sfIsFolder=FALSE;
  78.             reply.sfIsVolume=FALSE;
  79.         }
  80.     }
  81.  
  82.     if ((reply.sfGood) && (!isHuman))    /* make the actual FSSpec */
  83.         MyMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name,
  84.                             editFile);
  85.         
  86.     return ((reply.sfGood) && (!isHuman));    /* TRUE if we have a valid file selected */
  87. }
  88.  
  89. Boolean GetDestFile(FSSpec *destFS, Boolean *deleteTheThing, Str255 theTitle)
  90. /* a standard save dialog box -- given a title (for the prompt), it returns the
  91.    file's FSSpec in destFS and whether a file of that name already exists in
  92.    deleteTheThing (TRUE if file already exists) */
  93. /* details are pretty much the same as GetSourceFile(), see above */
  94. {
  95.     Str255                otherName;
  96.     StandardFileReply    reply;
  97.     SFReply                oldReply;
  98.     Point                where;
  99.     FInfo                fndrInfo;
  100.     long                procID;
  101.     OSErr                isHuman, err;
  102.     int                    i;
  103.         
  104.     isHuman=FALSE;
  105.     if (gStandardFile58)
  106.         StandardPutFile(theTitle, destFS->name, &reply);
  107.     else
  108.     {
  109.         where.h = (screenBits.bounds.right - 304)/2;
  110.         where.v = (screenBits.bounds.bottom - 184)/3;
  111.         SFPutFile(where, theTitle, destFS->name, 0, &oldReply);
  112.         reply.sfGood = oldReply.good;
  113.         if (reply.sfGood)
  114.         {
  115.             isHuman=GetWDInfo(oldReply.vRefNum, &reply.sfFile.vRefNum,
  116.                                 &reply.sfFile.parID, &procID);
  117.             if (isHuman!=noErr)
  118.             {
  119.                 reply.sfFile.vRefNum = oldReply.vRefNum;
  120.                 reply.sfFile.parID = 0;
  121.             }            
  122.             
  123.             Mymemcpy(reply.sfFile.name, oldReply.fName, oldReply.fName[0]+1);
  124.             
  125.             reply.sfScript = smSystemScript;
  126.             err=HGetFInfo(reply.sfFile.vRefNum, reply.sfFile.parID,
  127.                                 reply.sfFile.name, &fndrInfo);
  128.             reply.sfReplacing=(err!=fnfErr);
  129.             
  130.             reply.sfIsFolder=FALSE;
  131.             reply.sfIsVolume=FALSE;
  132.         }
  133.     }
  134.  
  135.     if ((reply.sfGood) && (!isHuman))
  136.     {
  137.         *deleteTheThing=reply.sfReplacing;    
  138.         MyMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name,
  139.                         destFS);
  140.     }
  141.     
  142.     return ((reply.sfGood) && (!isHuman));
  143. }
  144.  
  145. pascal OSErr MyMakeFSSpec(short vRefNum, long parID, Str255 fileName,
  146.     FSSpecPtr myFSS)
  147. /* a standard function for creating an FSSpec out of its three component parts.
  148.    if the system supports FSSpecs, we let it do the work; otherwise we have to
  149.    do it manually. */
  150. {
  151.     if (gHasFSSpecs)
  152.         return FSMakeFSSpec(vRefNum, parID, fileName, myFSS);
  153.     else
  154.     {
  155.         myFSS->vRefNum=vRefNum;
  156.         myFSS->parID=parID;
  157.         Mymemcpy(myFSS->name, fileName, fileName[0]+1);
  158.     }
  159.     
  160.     return noErr;
  161. }
  162.